namespace SinaFinance.SummaryInfoLibrary
{
    using SinaFinance.CtrlDelegate;
    using SinaFinanceBaseClassLibrary;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Net.NetworkInformation;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Xml.Linq;

    public partial class StockBasicInfo : UserControl
    {
        //private bool _contentLoaded;
        //internal Border borderDetail;
        //internal Border borderName;
        //internal Border borderSpan1;
        //internal Border borderSpan2;
        private XDocument doc;
        //internal Path DOWN;
        //internal Path DOWN1;
        private DB_SinaDataHelp Dsdh;
        private readonly Color grnColor = Color.FromArgb(0xff, 0x71, 0x8d, 5);
        //internal Canvas LayoutRoot;
        public bool LoadLocalFileSuccessfully = false;
        private readonly Color redColor = Color.FromArgb(0xff, 0xff, 0, 0);
        //internal EasingColorKeyFrame SpliColor1;
        //internal EasingColorKeyFrame SpliColor2;
        private string stockCode;
        private string stockName = string.Empty;
        private string stockNum = string.Empty;
        //internal Storyboard Storyboard1;
        //internal Storyboard TradeStory;
        //internal TextBlock txtAmount;
        //internal TextBlock txtChangePercent;
        //internal TextBlock txtHigh;
        //internal TextBlock txtLow;
        //internal TextBlock txtOpen;
        //internal TextBlock txtPriceChange;
        //internal TextBlock txtSettlement;
        //internal TextBlock txtStockCode;
        //internal TextBlock txtStockName;
        //internal TextBlock txtSwingPercent;
        //internal TextBlock txtTrade;
        //internal TextBlock txtUpdateTime;
        //internal TextBlock txtVolumn;
        //internal Path UP;
        //internal Path UP1;

        public StockBasicInfo()
        {
            this.InitializeComponent();
            this.Storyboard1.Begin();
            base.Loaded += new RoutedEventHandler(this.StockBasicInfo_Loaded);
            this.doc = XDocument.Load("PageConfig.xml");
        }

        private void Dsdh_GetMarketBaseDataCompleted(List<DB_RealTime> _DB_RealTime, string result)
        {
            if ((_DB_RealTime != null) && !_DB_RealTime.Count.Equals(0))
            {
                XElement element = (from labels in this.doc.Elements("Configs").Elements<XElement>("BaseInfoArea").Elements<XElement>("BaseInfoLabel") select labels).ToList<XElement>()[0];
                IntervalUpdates.db_rt = _DB_RealTime[0];
                IntervalUpdates.IntervalRscDelegate();
                IntervalUpdates.ts_IntervalTime = DateTime.Parse(_DB_RealTime[0].TickTime).ToString("HH:mm");
                this.txtStockName.Text = _DB_RealTime[0].Name;
                this.txtStockCode.Text = _DB_RealTime[0].Symbol;
                this.txtTrade.Text = Convert.ToDouble(_DB_RealTime[0].Trade).ToString("0.00");
                this.txtPriceChange.Text = element.Element("PriceChange").Value + Convert.ToDouble(_DB_RealTime[0].PriceChange).ToString("0.00");
                this.txtChangePercent.Text = element.Element("ChangePercent").Value + Convert.ToDouble(_DB_RealTime[0].ChangePercent).ToString("0.00") + element.Element("ChangePercentUnit").Value;
                this.txtUpdateTime.Text = DateTime.Parse(_DB_RealTime[0].UpdateTime).Date.ToString("yyyy-MM-dd") + " " + _DB_RealTime[0].TickTime;
                this.txtSettlement.Text = element.Element("Settlement").Value + Convert.ToDouble(_DB_RealTime[0].Settlement).ToString("0.00");
                this.txtOpen.Text = element.Element("Open").Value + Convert.ToDouble(_DB_RealTime[0].Open).ToString("0.00");
                this.txtHigh.Text = element.Element("High").Value + Convert.ToDouble(_DB_RealTime[0].High).ToString("0.00");
                this.txtLow.Text = element.Element("Low").Value + Convert.ToDouble(_DB_RealTime[0].Low).ToString("0.00");
                this.txtVolumn.Text = element.Element("Volumn").Value + (Convert.ToDouble(_DB_RealTime[0].Volumn) / 100.0) + element.Element("VolumnUnit").Value;
                this.txtAmount.Text = element.Element("Amount").Value + Convert.ToDouble((double) (Convert.ToDouble(_DB_RealTime[0].Amount) / 100000000.0)).ToString("0.00") + element.Element("AmountUnit").Value;
                this.txtSwingPercent.Text = element.Element("SwingPercent").Value + _DB_RealTime[0].SwingPercent + element.Element("SwingPercentUnit").Value;
                if (Convert.ToDouble(_DB_RealTime[0].PriceChange) >= 0.0)
                {
                    this.SpliColor1.Value = this.redColor;
                    this.SpliColor2.Value = this.redColor;
                    this.txtPriceChange.Foreground = new SolidColorBrush(this.redColor);
                    this.txtChangePercent.Foreground = new SolidColorBrush(this.redColor);
                    this.txtPriceChange.Text = "+ " + this.txtPriceChange.Text;
                    this.txtChangePercent.Text = "+ " + this.txtChangePercent.Text;
                    this.DOWN.Visibility = Visibility.Collapsed;
                    this.DOWN1.Visibility = Visibility.Collapsed;
                    this.UP.Visibility = Visibility.Visible;
                    this.UP1.Visibility = Visibility.Visible;
                }
                else
                {
                    this.SpliColor1.Value = this.grnColor;
                    this.SpliColor2.Value = this.grnColor;
                    this.txtPriceChange.Foreground = new SolidColorBrush(this.grnColor);
                    this.txtChangePercent.Foreground = new SolidColorBrush(this.grnColor);
                    this.txtPriceChange.Text = this.txtPriceChange.Text;
                    this.txtChangePercent.Text = this.txtChangePercent.Text;
                    this.UP.Visibility = Visibility.Collapsed;
                    this.UP1.Visibility = Visibility.Collapsed;
                    this.DOWN.Visibility = Visibility.Visible;
                    this.DOWN1.Visibility = Visibility.Visible;
                }
                this.TradeStory.Stop();
                this.TradeStory.Begin();
            }
        }

        public void GetBaseInfo(string codes)
        {
            if (!string.IsNullOrEmpty(codes))
            {
                this.stockCode = codes;
                if (this.Dsdh == null)
                {
                    this.Dsdh = new DB_SinaDataHelp();
                    this.Dsdh.GetMarketBaseDataCompleted += new GetMarketBaseDataHelp(this.Dsdh_GetMarketBaseDataCompleted);
                }
                this.Dsdh.GetMarketBaseData(this.stockCode);
            }
        }

        private void GetMarketDetailDataCompleted(List<DB_RealTime> _DB_RealTime)
        {
            if ((_DB_RealTime != null) && !_DB_RealTime.Count.Equals(0))
            {
                XElement element = (from labels in this.doc.Elements("Configs").Elements<XElement>("BaseInfoArea").Elements<XElement>("BaseInfoLabel") select labels).ToList<XElement>()[0];
                this.txtStockName.Text = _DB_RealTime[0].Name;
                this.txtStockCode.Text = _DB_RealTime[0].Symbol;
                this.txtTrade.Text = _DB_RealTime[0].Trade;
                this.txtPriceChange.Text = element.Element("PriceChange").Value + _DB_RealTime[0].PriceChange;
                this.txtChangePercent.Text = element.Element("ChangePercent").Value + _DB_RealTime[0].ChangePercent + element.Element("ChangePercentUnit").Value;
                this.txtUpdateTime.Text = _DB_RealTime[0].UpdateTime;
                this.txtSettlement.Text = element.Element("Settlement").Value + _DB_RealTime[0].Settlement;
                this.txtOpen.Text = element.Element("Open").Value + _DB_RealTime[0].Open;
                this.txtHigh.Text = element.Element("High").Value + _DB_RealTime[0].High;
                this.txtLow.Text = element.Element("Low").Value + _DB_RealTime[0].Low;
                this.txtVolumn.Text = element.Element("Volumn").Value + _DB_RealTime[0].Volumn + element.Element("VolumnUnit").Value;
                this.txtAmount.Text = element.Element("Amount").Value + Convert.ToDouble((double) (Convert.ToDouble(_DB_RealTime[0].Amount) / 100000000.0)).ToString("0.00") + element.Element("AmountUnit").Value;
                this.txtSwingPercent.Text = element.Element("SwingPercent").Value + _DB_RealTime[0].SwingPercent + element.Element("SwingPercentUnit").Value;
                if (Convert.ToDouble(_DB_RealTime[0].PriceChange) >= 0.0)
                {
                    this.txtTrade.Foreground = new SolidColorBrush(this.redColor);
                    this.txtPriceChange.Foreground = new SolidColorBrush(this.redColor);
                    this.txtChangePercent.Foreground = new SolidColorBrush(this.redColor);
                    this.txtPriceChange.Text = "+ " + this.txtPriceChange.Text;
                    this.txtChangePercent.Text = "+ " + this.txtChangePercent.Text;
                    this.DOWN.Visibility = Visibility.Collapsed;
                    this.DOWN1.Visibility = Visibility.Collapsed;
                    this.UP.Visibility = Visibility.Visible;
                    this.UP1.Visibility = Visibility.Visible;
                }
                else
                {
                    this.txtTrade.Foreground = new SolidColorBrush(this.grnColor);
                    this.txtPriceChange.Foreground = new SolidColorBrush(this.grnColor);
                    this.txtChangePercent.Foreground = new SolidColorBrush(this.grnColor);
                    this.txtPriceChange.Text = this.txtPriceChange.Text;
                    this.txtChangePercent.Text = this.txtChangePercent.Text;
                    this.UP.Visibility = Visibility.Collapsed;
                    this.UP1.Visibility = Visibility.Collapsed;
                    this.DOWN.Visibility = Visibility.Visible;
                    this.DOWN1.Visibility = Visibility.Visible;
                }
                this.LoadLocalFileSuccessfully = true;
            }
        }

        //[DebuggerNonUserCode]
        //public void InitializeComponent()
        //{
        //    if (!this._contentLoaded)
        //    {
        //        this._contentLoaded = true;
        //        Application.LoadComponent(this, new Uri("/SinaFinance;component/SummaryInfoLibrary/StockBasicInfo.xaml", UriKind.Relative));
        //        this.Storyboard1 = (Storyboard) base.FindName("Storyboard1");
        //        this.TradeStory = (Storyboard) base.FindName("TradeStory");
        //        this.SpliColor1 = (EasingColorKeyFrame) base.FindName("SpliColor1");
        //        this.SpliColor2 = (EasingColorKeyFrame) base.FindName("SpliColor2");
        //        this.LayoutRoot = (Canvas) base.FindName("LayoutRoot");
        //        this.borderName = (Border) base.FindName("borderName");
        //        this.borderDetail = (Border) base.FindName("borderDetail");
        //        this.UP = (Path) base.FindName("UP");
        //        this.UP1 = (Path) base.FindName("UP1");
        //        this.DOWN = (Path) base.FindName("DOWN");
        //        this.DOWN1 = (Path) base.FindName("DOWN1");
        //        this.txtTrade = (TextBlock) base.FindName("txtTrade");
        //        this.borderSpan1 = (Border) base.FindName("borderSpan1");
        //        this.borderSpan2 = (Border) base.FindName("borderSpan2");
        //        this.txtStockName = (TextBlock) base.FindName("txtStockName");
        //        this.txtStockCode = (TextBlock) base.FindName("txtStockCode");
        //        this.txtPriceChange = (TextBlock) base.FindName("txtPriceChange");
        //        this.txtChangePercent = (TextBlock) base.FindName("txtChangePercent");
        //        this.txtSettlement = (TextBlock) base.FindName("txtSettlement");
        //        this.txtOpen = (TextBlock) base.FindName("txtOpen");
        //        this.txtHigh = (TextBlock) base.FindName("txtHigh");
        //        this.txtLow = (TextBlock) base.FindName("txtLow");
        //        this.txtUpdateTime = (TextBlock) base.FindName("txtUpdateTime");
        //        this.txtVolumn = (TextBlock) base.FindName("txtVolumn");
        //        this.txtAmount = (TextBlock) base.FindName("txtAmount");
        //        this.txtSwingPercent = (TextBlock) base.FindName("txtSwingPercent");
        //    }
        //}

        public void LoadLocalFile()
        {
            this.GetMarketDetailDataCompleted(LocalFileStorage.ReadMarketDetailInfoFile());
        }

        public void setBorderHeight(double bd1Height, double bd2Height)
        {
            this.borderName.Height = bd1Height;
            this.borderDetail.Height = bd2Height;
        }

        public void setBorderWidth(double bd1Width, double bd2Width)
        {
            this.borderName.Width = bd1Width;
            this.borderDetail.Width = bd2Width;
        }

        private void StockBasicInfo_Loaded(object sender, RoutedEventArgs e)
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                IntervalUpdates.SetDelegate(new SinaFinance.CtrlDelegate.UpdateBaseInfoDelegate(this.UpdateBaseInfoByInterval));
                IntervalUpdates.Start();
            }
        }

        public void UpdateBaseInfoByInterval()
        {
            if (!string.IsNullOrEmpty(this.stockCode))
            {
                if (this.Dsdh == null)
                {
                    this.Dsdh = new DB_SinaDataHelp();
                    this.Dsdh.GetMarketBaseDataCompleted += new GetMarketBaseDataHelp(this.Dsdh_GetMarketBaseDataCompleted);
                }
                this.Dsdh.GetMarketBaseData(this.stockCode);
            }
        }

        public delegate void UpdateBaseInfoDelegate();
    }
}
